package com.gistone.commis.api.restController;

import com.gistone.commis.dto.EditorResult;
import com.gistone.commis.dto.FieldError;
import com.gistone.commis.dto.Option;
import com.gistone.commis.dto.Options;
import com.gistone.commis.entity.*;
import com.gistone.commis.mapper.*;
import com.gistone.commis.util.DataTablePageUtil;
import com.gistone.commis.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController("apiPersonLogController")
@RequestMapping("api/personLog")
public class PersonLogController {

    private static final Logger logger = LoggerFactory.getLogger(PersonLogController.class);


    @Autowired
    private PersonLogMapper personLogMapper;
    @Autowired
    private PersonCcMapper personCcMapper;
    @Autowired
    private PositionTypeMapper positionTypeMapper;




    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private DepartMapper departMapper;




    /**
     * dataTables 表单数据返回
     *
     * @param request
     * @return
     */

    @Transactional
    @RequestMapping(method = RequestMethod.GET, value = "/pageDatatables")
    public DataTablePageUtil<PersonLog> pageDatatables(HttpServletRequest request, PersonLog personLog) {
//		HttpSession httpSession = request.getSession();
//		User user = (User) httpSession.getAttribute("userInfo");
//		logger.info("用户信息：" + user.toString());
//		int community_id = user.getCommunity().getCommunityId();
//		personLog.setCommunityId(community_id);

        List<Community> communities = communityMapper.findCommunitys();
        Options os = new Options();
        List<Option> optionList = new ArrayList<>();
        for (Community community : communities) {

            Option option = new Option();
            option.setLabel(community.getCommunityName());
            option.setValue(community.getCommunityId());
            optionList.add(option);


        }
        os.setCommunityId(optionList);
        List<Depart> departs = departMapper.findDeparts();
        optionList = new ArrayList<>();
        for (Depart depart : departs) {

            Option option = new Option();
            option.setLabel(depart.getDepartName());
            option.setValue(depart.getDepartId());
            optionList.add(option);


        }

        os.setDepartId(optionList);

        List<PositionType> positionTypes = positionTypeMapper.findPositionTypes();
        optionList = new ArrayList<>();
        for (PositionType positionType : positionTypes) {

            Option option = new Option();
            option.setLabel(positionType.getPositionName());
            option.setValue(positionType.getPositionId());
            optionList.add(option);


        }
        os.setPositionId(optionList);


        List<PersonCc> personCcs = personCcMapper.findPersonCcs();
        optionList = new ArrayList<>();
        for (PersonCc personCc : personCcs) {

            Option option = new Option();
            option.setLabel(personCc.getCcName());
            option.setValue(personCc.getCcId());
            optionList.add(option);


        }
        os.setCcId(optionList);

        DataTablePageUtil<PersonLog> dataTable = new DataTablePageUtil<>(request);

        PageHelper.startPage(dataTable.getPage_num(), 10);
        List<PersonLog> list = personLogMapper.selectSelective(personLog);
        for (PersonLog personLog1 : list) {
            System.out.println(personLog1.getSWorkDate());

        }
        PageInfo<PersonLog> pageInfo = new PageInfo<>(list);
        System.out.println(list.size());
        dataTable.setDraw(dataTable.getDraw());
        dataTable.setData(pageInfo.getList());
        dataTable.setOptions(os);
        dataTable.setRecordsTotal((int) pageInfo.getTotal());
        dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
        //返回数据到页面

        return dataTable;

    }







}
